package servlets;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.User;

import managers.UserManager;

/**
 * Servlet implementation class Users
 */
@WebServlet("/Users")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    @EJB
    UserManager userManager;
    
    public UsersServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//get list of users
		List<User> users = userManager.getUsers();
		request.setAttribute("users", users);
		request.getRequestDispatcher("WEB-INF/ManageUsers.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		User u = userManager.getUser(id);
		
		request.setAttribute("user", u);
		request.getRequestDispatcher("WEB-INF/AccountManage.jsp").forward(request, response);
	}

}
